<template>
  <home-panel title="热门品牌" subTitle="国际经典 品质保证">
    <template #right>
      <a href="javascript:;" class="iconfont icon-angle-left prev" :class="{disabled:index===0}" @click="toggle(-1)"></a>
      <a href="javascript:;" class="iconfont icon-angle-right next" :class="{disabled:index===1}" @click="toggle(1)"></a>
    </template>

    <div class="box" ref="target">
    <transition name="fade">
      <ul class="list" v-if="brands.length" :style="{transform:`translateX(${-index*1240}px)`}">
        <li v-for="item in brands" :key="item.id">
          <router-link to="/">
            <img :src="item.picture" alt="">
          </router-link>
        </li>
      </ul>

      <div class="skeleton" v-else>
        <XtxSkeleton class="item" v-for="i in 5" :key="i" animated bg="#e4e4e4" width="240px" height="305px"/>
      </div>
    </transition>

    </div>
  </home-panel>
</template>

<script>
import HomePanel from '@/views/home/components/home-panel'
import { findBrand } from '@/api/home.js'
import { useLazyData } from '@/hooks/index.js'
import { ref } from 'vue'
export default {
  name: 'HomeBrand',
  components: {
    HomePanel
  },

  setup () {
    // const brands = ref([])

    // findBrand().then(data => {
    //   brands.value = data.result
    // })

    const { result, target } = useLazyData(() => findBrand(10))

    // 箭头切换效果，业务说明，只有两页，0和1
    const index = ref(0)

    const toggle = (step) => {
      const newIndex = index.value + step

      if (newIndex > 1 || newIndex < 0) return

      index.value = newIndex
    }

    return { brands: result, toggle, index, target }
  }

}
</script>

<style lang="less" scoped>
.home-panel {
  background:#f5f5f5
}
.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;
  background: @xtxColor;
  &::before {
    font-size: 12px;
    position: relative;
    top: -2px
  }
  &.disabled {
    background: #ccc;
    cursor: not-allowed;
  }
}
.box {
  display: flex;
  width: 100%;
  height: 345px;
  overflow: hidden;
  padding-bottom: 40px;
  .list {
    width: 200%;
    display: flex;
    transition: all 1s;
    li {
      margin-right: 10px;
      width: 240px;
      &:nth-child(5n) {
        margin-right: 0;
      }
      img {
        width: 240px;
        height: 305px;
      }
    }
  }

  .skeleton {
    width: 100%;
    display: flex;
    .item {
      margin-right: 10px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}

</style>
